package com.share.controller;

import com.share.pojo.User;
import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;

@Controller
@RequestMapping("/file")
public class FileController {

    /*
    http://localhost:8080/file/add--->file.jsp--->http://localhost:8080/file/upload--->hello.jsp

     */
    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String add() {
        System.out.println("###add");
        return "file";
    }

    /*
    文件上传所需内容：bean：class="org.springframework.web.multipart.commons.CommonsMultipartResolver"
                   jar包：commons-fileupload
     */
    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public String upload(@RequestParam(value = "file")MultipartFile file) throws Exception{

        System.out.println("###upload");
        if(!file.isEmpty()) {//判断的是文件内容
            System.out.println("###file:"+file.getOriginalFilename());
            FileUtils.copyInputStreamToFile(file.getInputStream(),
                    new File("F:\\实验\\",System.currentTimeMillis()+file.getOriginalFilename()));
        }
        return "hello";
    }
}
